package com.kingja.loadsir.callback;

import com.kingja.loadsir.utils.TextUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.app.Context;
import com.kingja.loadsir.ResourceTable;


public class ProgressCallback extends Callback {
    private AnimatorValue animator;
    private String title;

    private ProgressCallback(Builder builder) {
        this.title = builder.title;
    }

    @Override
    protected int onCreateView() {
        return 0;
    }

    @Override
    protected Component onBuildView(Context context) {
        return LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_progress,null,false);
    }

    @Override
    protected void onViewCreate(Context context, Component view) {

        RoundProgressBar bar= (RoundProgressBar) view.findComponentById(ResourceTable.Id_loading_bar);
        Text text= (Text) view.findComponentById(ResourceTable.Id_sub_title);
        if(!TextUtils.isEmpty(title)){
            text.setText(title);
        }
        animator = new AnimatorValue();
        animator.setDuration(1500);
        animator.setLoopedCount(-1);
        animator.setCurveType(Animator.CurveType.ACCELERATE);

        animator.setValueUpdateListener((animatorValue, value) -> {
            bar.setRotation((int) (360 * value));
        });
        animator.start();
    }

    public static class Builder {

        private String title;

        public Builder setTitle(String title  ) {
            this.title = title;
            return this;
        }

        public ProgressCallback build() {
            return new ProgressCallback(this);
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
        if(animator!=null){
            animator.stop();
            animator=null;
        }
    }

}